package aes.gui;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import aes.general.GuiControlBeta;
import aes.general.Teacher;
import aes.general.TeacherControl;

/**
 * The Class TeacherFrame. This is the frame of the teacher. 
 * This frame provides every option the teacher have
 * according to the requirements.
 */
public class TeacherFrame extends JFrame {

	/**
	 *   
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane = null;
	private JPanel mainPanel = null;
	private JPanel Actpanel;
	private JPanel Statpanel;
	private JPanel Requestpanel;
	private JPanel LockExampanel;
	private JPanel ApprovePanel;
	private JPanel qsp;
	private JPanel exampanel;
	private JLabel lbWellcome;
	private JButton btnaddQ;
	private JButton btncrtExm;
	private JButton btnActiveExam;
	private JButton btnLogOut;
	private JButton btnStat;
	private JButton b1;
	private JButton b2;
	private JButton b3;
	
	/**
	 * Create the frame.
	 *
	 * @param teacher the teacher
	 */
	public TeacherFrame(Teacher teacher) {
		super();
		initialize();
	}

	/**
	 * Initialize.
	 */
	private void initialize() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(100, 100, 640, 490);
		this.setTitle("Teacher");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new CardLayout(0, 0));

		mainPanel = new JPanel();
		contentPane.add(mainPanel, "mainPanel");
		
		qsp = new TeacherQuestionPanel(contentPane);
		contentPane.add(qsp, "QuestionPanel");
		
		mainPanel.setLayout(null);
		exampanel = new TeacherExamPanel(contentPane);
		contentPane.add(exampanel,"ExamPanel");
		
		
		Actpanel = new TeacherActivationPanel(contentPane);
		contentPane.add(Actpanel, "ActivPanel");

		Requestpanel = new TeacherRequestPanel(contentPane);
		contentPane.add(Requestpanel,"Requests");
		
		LockExampanel = new TeacherLockPanel(contentPane);
		contentPane.add(LockExampanel,"lock");
		
		
		lbWellcome = new JLabel("Welcome");
		lbWellcome.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		lbWellcome.setBounds(258, 11, 98, 29);
		mainPanel.add(lbWellcome);

		btncrtExm = new JButton("Create Exam");
		btncrtExm.setBounds(337, 88, 120, 23);
		mainPanel.add(btncrtExm);
		btncrtExm.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0){
				CardLayout cl = (CardLayout)(contentPane.getLayout());
				cl.show(contentPane,"ExamPanel");
			}
		});
		
		btnActiveExam = new JButton("Activate Exam");
		btnActiveExam.setBounds(133, 137, 139, 23);
		mainPanel.add(btnActiveExam);
		btnActiveExam.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					Thread.sleep(400);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}			
				CardLayout cl = (CardLayout) (contentPane.getLayout());
				cl.show(contentPane, "ActivPanel");
				
			}
		});
		
		btnaddQ = new JButton("Add Questions");
		btnaddQ.setBounds(133, 88, 139, 23);
		mainPanel.add(btnaddQ);
		btnaddQ.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cl = (CardLayout) (contentPane.getLayout());
				cl.show(contentPane, "QuestionPanel");
			}
		});
		
		btnLogOut = new JButton("Log out");
		btnLogOut.setBounds(250, 297, 113, 23);
		mainPanel.add(btnLogOut);
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFrame login = new LoginFrame();
				dispose();
				login.setVisible(true);
			}
		});
		
		btnStat = new JButton("View Statistics");
		btnStat.setBounds(337, 185, 120, 23);
		mainPanel.add(btnStat);
		btnStat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TeacherControl.RequestStatExams(GuiControlBeta.userid);
				Statpanel = new TeacherStatPanel(contentPane);
				contentPane.add(Statpanel, "Stat");
				CardLayout cl = (CardLayout) (contentPane.getLayout());
				cl.show(contentPane, "Stat");
			}
		});
		
		b1 = new JButton("Lock Exam");
		b1.setBounds(337, 137, 120, 23);
		mainPanel.add(b1);
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cl = (CardLayout) (contentPane.getLayout());
				cl.show(contentPane, "lock");
			}
		});
		
		b2 = new JButton("Extend Exam Time");
		b2.setBounds(133, 185, 139, 23);
		mainPanel.add(b2);
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cl = (CardLayout) (contentPane.getLayout());
				cl.show(contentPane, "Requests");
			}
		});
	////////////////////////////////////////////	
		b3 = new JButton("Approve Grades");
		b3.setBounds(133, 235, 139, 23);
		mainPanel.add(b3);
		b3.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0){
				
				TeacherControl.GetNonApprovedExams(GuiControlBeta.userid);
				while(TeacherControl.getNonappdExams() == null);
				
				if(TeacherControl.getNonappdExams().get(0).equals("error"))
				{
					JOptionPane.showMessageDialog(null, "Nothing to Approve",
							"OK", JOptionPane.INFORMATION_MESSAGE);
				}
				else{
					ApprovePanel = new TeacherApproveGradePanel(contentPane);
					contentPane.add(ApprovePanel,"approve");
					CardLayout cl = (CardLayout)(contentPane.getLayout());
					cl.show(contentPane,"approve");
				}
			}
		});
		
	////////////////////////////////////////////////////////	
		
	}
}
